package com.lagou.service;

import com.lagou.bean.Person;
import com.lagou.bean.Relation;
import com.lagou.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("personService")
public class Neo4jPersonService {
    @Autowired
    private PersonRepository personRepository;

    public List<Person> getAll() {
        List<Person> datas = new ArrayList<>();
        personRepository.findAll().forEach(person -> datas.add(person));
        return datas;
    }

    public List<Person> personList(double money) {
        return personRepository.personList(money);
    }

    public List<Person> shortestPath(String startName, String endName) {
        return personRepository.shortestPath(startName, endName);
    }

    public List<Relation> personListDept(String startName, String endName) {
        List<Relation> relations = new ArrayList<>();
        List<Person> people = personRepository.shortestPath(startName,endName);
        for (Person person : people) {
            if (person.getFriendsPerson() != null && person.getFriendsPerson().size() > 0) {
                for (Person friend : person.getFriendsPerson()) {
                    Relation relation = new Relation();
                    relation.setSource(person.getName());
                    relation.setTarget(friend.getName());
                    relation.setType("resolved");
                    relation.setRela("朋友");
                    relations.add(relation);
                }
            }
            if (person.getBrotherSister() != null && person.getBrotherSister().size() > 0) {
                for (Person brother : person.getBrotherSister()) {
                    Relation relation = new Relation();
                    relation.setSource(person.getName());
                    relation.setTarget(brother.getName());
                    relation.setType("resolved");
                    relation.setRela("兄妹");
                    relations.add(relation);
                }
            }
            if (person.getFather() != null && person.getFather().size() > 0) {
                for (Person father : person.getFather()) {
                    Relation relation = new Relation();
                    relation.setSource(person.getName());
                    relation.setTarget(father.getName());
                    relation.setType("resolved");
                    relation.setRela("父子");
                    relations.add(relation);
                }
            }
            if (person.getWife_Mother() != null && person.getWife_Mother().size() > 0) {
                for (Person wifeMother : person.getWife_Mother()) {
                    Relation relation = new Relation();
                    relation.setSource(person.getName());
                    relation.setTarget(wifeMother.getName());
                    relation.setType("resolved");
                    relation.setRela("岳母");
                    relations.add(relation);
                }
            }
        }
        return relations;
    }


}
